//
//  NoticeDetailViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/1.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "NoticeDetailViewController.h"

@interface NoticeDetailViewController () <WKNavigationDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, assign) CGFloat delayTime;
@property (nonatomic, assign) CGFloat contentHeight;

@end

@implementation NoticeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNoticeDetailView];
}

-(void)createNoticeDetailView
{
    self.titleLabel.text = @"车盟公告";
    self.backButton.hidden = NO;

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight-self.baseBarHeight)];
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;
    
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200) configuration:wkWebConfig];
    self.webView.navigationDelegate = self;
    [_scrollView addSubview:self.webView];
    [self.webView loadHTMLString:self.content baseURL:[NSURL URLWithString:WEBSERVICEDOMAIN]]; 
//    [self.webView loadHTMLString:@"<p>1<img alt=\"\" src=\"/cm/resource/upload/images/2017/11/30/1512010332281016.jpg\" style=\"height:388px; width:388px\" /></p>" baseURL:[NSURL URLWithString:WEBSERVICEDOMAIN]];
    
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, 2)];
    [self.view addSubview:self.progressView];
    self.progressView.progressTintColor = [UIColor colorWithRed:0.95 green:0.76 blue:0.18 alpha:1];
    self.progressView.trackTintColor = [UIColor clearColor];
    
    // KVO 监听属性
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    // 监听 self.webView.scrollView 的 contentSize 属性改变，从而对底部添加的自定义 View 进行位置调整
    [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)dealloc
{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        [self.progressView setProgress:self.webView.estimatedProgress animated:YES];
        if (self.webView.estimatedProgress < 1.0) {
            self.delayTime = 1 - self.webView.estimatedProgress;
        }
        else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.progressView.progress = 0;
            });
        }
    }
    else if ([keyPath isEqualToString:@"contentSize"]) {
        if (self.contentHeight != self.webView.scrollView.contentSize.height) {
            self.contentHeight = self.webView.scrollView.contentSize.height;
            self.webView.frame = CGRectMake(0, 0, kScreenWidth, self.contentHeight);
            [_scrollView setupAutoContentSizeWithBottomView:self.webView bottomMargin:25];
            NSLog(@"self.contentHeight----------%f", self.contentHeight);
        }
    }
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    NSLog(@"webViewDidFinishLoad");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
